﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
' <copyright file="SizeConstrainer.vb" company="Microsoft">
'     Copyright © Microsoft Corporation. All rights reserved.
' </copyright>
' <summary>Implements the SizeConstrainer class</summary>
' <author>Microsoft Expression Encoder Team</author>using System;

Imports Microsoft.VisualBasic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Markup

Namespace ExpressionMediaPlayer
	''' <summary>
	''' Wrapper Control that prevents the content it contains from expanding when 
	''' offered infinity eg when inside a table cell with * height.
	''' </summary>
	<ContentProperty("Child")> _
	Public Class SizeConstrainer
		Inherits Control
		''' <summary>
		''' A child dependency property.
		''' </summary>
		Public Shared ReadOnly ChildProperty As DependencyProperty = DependencyProperty.Register ("Child", GetType(UIElement), GetType(SizeConstrainer), New PropertyMetadata(New PropertyChangedCallback(AddressOf SizeConstrainer.OnChildPropertyChanged)))
		''' <summary>
		''' The content presenter.
		''' </summary>
		Private m_presenter As ContentPresenter
		''' <summary>
		''' Initializes a new instance of the SizeConstrainer class.
		''' </summary>
		Public Sub New()
			DefaultStyleKey = GetType(SizeConstrainer)
		End Sub
		''' <summary>
		''' Gets or sets the child UI element.
		''' </summary>
		Public Property Child() As UIElement
			Get
				Return TryCast(GetValue(ChildProperty), UIElement)
			End Get
			Set(ByVal value As UIElement)
				SetValue(ChildProperty, value)
			End Set
		End Property
		''' <summary>
		''' Event handler for the child dependency property.
		''' </summary>
		''' <param name="dobj">Dependency object that is changing.</param>
		''' <param name="args">Event args.</param>
		Private Shared Sub OnChildPropertyChanged(ByVal dobj As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
			TryCast(dobj, SizeConstrainer).OnChildChanged()
		End Sub
		''' <summary>
		''' Event handler for the on child changed event.
		''' </summary>
		Private Sub OnChildChanged()
			If m_presenter IsNot Nothing Then
				m_presenter.Content = Child
			End If
		End Sub
		''' <summary>
		''' Overrides MeasureOverride().
		''' </summary>
		''' <param name="availableSize">The available size.</param>
		''' <returns>The size measured.</returns>
		Protected Overrides Function MeasureOverride(ByVal availableSize As Size) As Size
			'Important that we allow the child to measure to the true available size rather than 0,0..
			Child.Measure(availableSize)

			'But we return a zero size requirement
			'rather than the result of the measure operation
			'(The crucial line of code in this class)
			Return New Size(0, 0)
		End Function
		''' <summary>
		''' Overrides ArrangeOverride().
		''' </summary>
		''' <param name="finalSize">The final size.</param>
		''' <returns>The arranged size.</returns>
		Protected Overrides Function ArrangeOverride(ByVal finalSize As Size) As Size
			Return MyBase.ArrangeOverride(finalSize)
		End Function
		''' <summary>
		''' Overrides OnApplyTemplate().
		''' </summary>
		Public Overrides Sub OnApplyTemplate()
			MyBase.OnApplyTemplate()
			m_presenter = TryCast(GetTemplateChild("ChildPresenter"), ContentPresenter)
			OnChildChanged()
		End Sub
	End Class
End Namespace
